//
//  MapDestinationDetailsViewController.m
//  LZMPro
//
//  Created by dllo on 15/11/7.
//  Copyright © 2015年 LZS. All rights reserved.
//

#import "MapDestinationDetailsViewController.h"
#import "MapDesCollectionViewCell.h"
#import "MapDesElementsModel.h"
#import "MapCityViewController.h"
#import "MapDesCItyModel.h"
#import <AFNetworking.h>


@interface MapDestinationDetailsViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,DestinationDelegate,UIScrollViewDelegate>

@property (nonatomic,strong) NSMutableArray *dataArray;

@property (nonatomic,retain)NSTimer *timer;

@property (nonatomic,assign) float cureentColor;


@end

@implementation MapDestinationDetailsViewController

- (void)dealloc {
    
    _destinationView.myCollectionView.delegate = nil;
    _destinationView.myCollectionView.dataSource = nil;
}


- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    self.navigationController.navigationBar.translucent = YES;
    
    self.timer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(changeMycolor) userInfo:nil repeats:YES];
    
    
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
    
    [self.timer invalidate];
     self.timer = nil;
  
    
}



-(void)changeMycolor
{
    
    if (self.timer == nil) return;
    
    self.cureentColor += 0.01;
    
    [[[self.navigationController.navigationBar subviews]objectAtIndex:0] setAlpha: (1.0 - self.cureentColor)];
    
    
    NSLog(@"555555%f",self.cureentColor);
    if( ABS(self.cureentColor - 1.0)<= 0.05 ) {
        
        [[[self.navigationController.navigationBar subviews]objectAtIndex:0] setAlpha: 0];
        
        [self.timer invalidate];
        self.timer = nil;
        
    }
    
    
    
}

-(void)push_album
{
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    UIImage *image = [UIImage imageNamed:@"add_new_poi_back_btn"];
    UIImage *img = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIBarButtonItem *left = [[UIBarButtonItem alloc]initWithImage:img style:UIBarButtonItemStylePlain target:self action:@selector(pop)];
    self.navigationItem.leftBarButtonItem = left;
    
    self.dataArray = [NSMutableArray array];
    
    self.destinationView = [[DestinationView alloc]initWithFrame:self.view.bounds];
    self.destinationView.myCollectionView.delegate = self;
    self.destinationView.myCollectionView.dataSource = self;
    self.destinationView.delegate = self;
    [self.destinationView.myCollectionView registerClass:[MapDesCollectionViewCell class] forCellWithReuseIdentifier:@"DestinationCell"];
    
    [self.destinationView.headImage sd_setImageWithURL:[NSURL URLWithString:self.imageUrl] placeholderImage:nil];
    
    [self.view addSubview:self.destinationView];
    
     [self getData];
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.dataArray.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    MapDesCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"DestinationCell" forIndexPath:indexPath];
    
    
   
    
    MapDesCItyModel *model = self.dataArray[indexPath.row];
    cell.model = model;
    
    
    return cell;
    
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    NSString *urlStr;
    NSString *type;
    NSString *imageUrl;
    
 
    
    MapDesCItyModel *model = self.dataArray[indexPath.row];
    
    
    
    
    
    urlStr = model.ID;
    type = model.type;
    imageUrl = model.cover;
    
    if ([type integerValue] != 5) {
        MapDestinationDetailsViewController *detailVC = [[MapDestinationDetailsViewController alloc]init];
        detailVC.imageUrl = imageUrl;
        detailVC.UrlStr = [NSString stringWithFormat:@"http://api.breadtrip.com/destination/place/%@/%@", type, urlStr];
        [self.navigationController pushViewController:detailVC animated:YES];
        
    }
    else{
        
        MapCityViewController *city = [[MapCityViewController alloc]init];
        city.UrlStr = [NSString stringWithFormat:@"http://api.breadtrip.com/destination/place/5/%@", urlStr];
        city.model = model;
        [self.navigationController pushViewController:city animated:YES];
        
    }
    
    
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{

    if (self.destinationView.delegate == nil) return;
    
    CGFloat y = scrollView.contentOffset.y;
    
    if (y < -HEIGHT * 0.3) {
        CGRect frame = self.destinationView.headImage.frame;
        frame.origin.y = y;//imageView的frame是不断往上偏移
        frame.size.height = -y;//tablview向下偏移了多少,高度就增加多少
        self.destinationView.headImage.frame = frame;
        
        NSLog(@"3333333%f",y);
        
        
    }
    CGFloat naviAlpha = (HEIGHT*0.3 + y + 64)/(HEIGHT*0.3);
    [[[self.navigationController.navigationBar subviews]objectAtIndex:0] setAlpha:naviAlpha];
}

-(void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{
    CGFloat y = scrollView.contentOffset.y;
    if (y < - HEIGHT * 0.45) {
        
        [self push_album];
    }
}

-(void)pop
{
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)getData{
    
    
    
    AFHTTPSessionManager* manager = [AFHTTPSessionManager manager];
    
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    [manager.requestSerializer setValue:@"BreadTrip/6.1.1/zh (iPhone7,1; iPhone OS8.1.3; zh-Hans zh_CN) Paros/3.2.13" forHTTPHeaderField:@"User-Agent"];
    [manager GET:self.UrlStr parameters:nil success:^(NSURLSessionDataTask *operation, id responseObject) {
      
        
        NSArray *array = [responseObject objectForKey:@"hottest_sites"];
        self.dataArray = [NSMutableArray arrayWithArray:array];
        NSLog(@"%@",array);
        
         
        for (int i = 0;  i < self.dataArray.count; i ++) {
            
            NSDictionary *dic = self.dataArray[i];
            
            MapDesCItyModel *model = [[MapDesCItyModel alloc]init];
            
            [model setValuesForKeysWithDictionary:dic];
            
            [self.dataArray replaceObjectAtIndex:i withObject:model];
            
            
            
        }
        
        
        
        [self.destinationView.myCollectionView reloadData];

        
        
           } failure:^(NSURLSessionDataTask *operation, NSError *error) {
        NSLog(@"失败==== %@",error);
    }];


    
    
}


@end
